package com.example.hyw.szysteam.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.hyw.szysteam.POJO.Review;
import com.example.hyw.szysteam.POJO.UserInfo;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.adapter.ReviewAdapter;
import com.example.hyw.szysteam.presenter.SaveMistakeBookPresenter;
import com.example.hyw.szysteam.utils.ShowTips;
import com.google.gson.Gson;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.ArrayList;

/**
 * Created by HYW on 2018/7/9.
 */

public class ReviewActivity extends AppCompatActivity {
    QMUITopBar mTopBar;
    ArrayList<Review> reviews;
    ListView listView;
    SaveMistakeBookPresenter saveMistakeBookPresenter=new SaveMistakeBookPresenter(ReviewActivity.this);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review);
        Bundle bundle=getIntent().getExtras();
        reviews=bundle.getParcelableArrayList("reviews");
        initTopBar();
        initView();
    }

    private void initView(){
        listView=findViewById(R.id.listview);
        ReviewAdapter adapter=new ReviewAdapter(ReviewActivity.this,R.layout.item_review,reviews);
        listView.setAdapter(adapter);
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (reviews.get(i).getCollect()==0) {
                    saveMistakeBookPresenter.saveMistakeBook(UserInfo.getEmail(), reviews.get(i).getId(),reviews.get(i).getMyAnswer());
                    reviews.get(i).setCollect(1);
                    return true;
                }else {
                    error();
                    return false;
                }
            }
        });
    }

    private void initTopBar(){
        mTopBar=findViewById(R.id.topbar);
        mTopBar.setTitle("查看题目").setTextColor(getResources().getColor(R.color.myWhite));
        mTopBar.addLeftImageButton(R.drawable.ic_left,R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTopBar.addRightImageButton(R.drawable.ic_more,R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new QMUIBottomSheet.BottomListSheetBuilder(ReviewActivity.this)
                        .addItem("收藏所有错题")

                        .setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                            @Override
                            public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                                dialog.dismiss();
                                switch (position){
                                    case 0:
                                    default:
                                        ArrayList<Integer> list=new ArrayList<>();
                                        ArrayList<String> myanswer=new ArrayList<>();
                                        for (Review r:reviews){
                                            int i;
                                            String s;
                                            if (!r.getAnswer().equals(r.getMyAnswer()) && r.getCollect()!=1){
                                                i=r.getId();
                                                s=r.getMyAnswer();
                                                myanswer.add(s);
                                                list.add(i);
                                                r.setCollect(1);
                                            }
                                        }
                                        Gson gson=new Gson();
                                        String tmp=gson.toJson(list);
                                        String tmp1=gson.toJson(myanswer);
                                        saveMistakeBookPresenter.saveMistakeBooks(UserInfo.getEmail(),tmp,tmp1);
                                }
                            }
                        })
                        .build()
                        .show();
            }
        });
    }

    public void success1(){
        Toast.makeText(ReviewActivity.this,"收藏成功!",Toast.LENGTH_SHORT).show();
    }

    public void success2(int i){
        ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_SUCCESS,"成功收藏"+i+"题错题!",listView,ReviewActivity.this);
    }

    public void error(){
        Toast.makeText(ReviewActivity.this,"该题已收藏!",Toast.LENGTH_SHORT).show();
    }

    public void error2(){
        ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_SUCCESS,"错题已全部收藏!",listView,ReviewActivity.this);
    }
}
